package com.justgame.project.service.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.system.entity.SysHomeColumn;
import com.justgame.project.service.system.dto.SysHomeColumnDTO;
import com.justgame.project.service.system.wrapper.SysHomeColumnWrapper;
import com.justgame.project.service.system.controller.form.SysHomeColumnQueryForm;
import com.justgame.project.service.system.service.ISysHomeColumnService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 首页菜单栏目配置表控制器
 *
 * @@tableName sys_home_column
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/home/column")
@Api("首页菜单栏目配置表控制器")
public class SysHomeColumnController {

    private final ISysHomeColumnService sysHomeColumnService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 首页菜单栏目配置表")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(sysHomeColumnService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 首页菜单栏目配置表")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated SysHomeColumnDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getTitle(),"必须提供标题");
        Assert.notNull(dto.getLogoType(),"必须提供板块类型");
        Assert.notNull(dto.getLinkPath(),"必须提供链接地址");
        Assert.notNull(dto.getDescription(),"必须提供描述");
        SysHomeColumn sysHomeColumn = sysHomeColumnService.requireGetById(dto.getId(),"相关数据不存在");
        sysHomeColumn.setTitle(dto.getTitle());
        sysHomeColumn.setLogoType(dto.getLogoType());
        sysHomeColumn.setLinkPath(dto.getLinkPath());
        sysHomeColumn.setDescription(dto.getDescription());
        return Result.status(sysHomeColumnService.updateById(sysHomeColumn));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 首页菜单栏目配置表")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated SysHomeColumnDTO dto){
        Assert.notNull(dto.getTitle(),"必须提供标题");
        Assert.notNull(dto.getLogoType(),"必须提供板块类型");
        Assert.notNull(dto.getLinkPath(),"必须提供链接地址");
        Assert.notNull(dto.getDescription(),"必须提供描述");
        SysHomeColumn sysHomeColumn = new SysHomeColumn();
        sysHomeColumn.setTitle(dto.getTitle());
        sysHomeColumn.setLogoType(dto.getLogoType());
        sysHomeColumn.setLinkPath(dto.getLinkPath());
        sysHomeColumn.setDescription(dto.getDescription());
        return Result.status(sysHomeColumnService.save(sysHomeColumn));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 首页菜单栏目配置表")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(sysHomeColumnService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") SysHomeColumnQueryForm queryForm){
        IPage<SysHomeColumnWrapper> page = sysHomeColumnService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") SysHomeColumnQueryForm queryForm){
        List<SysHomeColumnWrapper> list = sysHomeColumnService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}